var vm = new Vue({
    el: '#container'
    , data: {
        buttons: {}
        , query: {
            page: 1
            , pageSize: hcCore.pageSize
            , companyNo: ''
            , mNo: ''
            , mType: ''
            , factoryNo: ''
        }
        , tableData: {
            records: []
            , total: 0
        }
        , factories: []
        , mtypes: []
        , edit: {
            formId: 'editFrame'
            , title: '操作'
            , visible: false
            , fields: [{
                label: '设备编号'
                , name: 'pkid'
                , require: true
                , type: 'hidden'
                , disabled: false
                , value: ''
                , rules: {
                    required: true, message: '', trigger: 'blur'
                }
            },{
                label: '设备编号'
                , name: 'm_no'
                , require: true
                , type: 'text'
                , disabled: false
                , value: ''
                , placeholder: '请输入设备编号'
                , rules: {
                    required: true, message: '设备编号不能为空', trigger: 'blur'
                }
            }, {
                label: 'IP地址'
                , name: 'ip'
                , require: true
                , type: 'text'
                , disabled: false
                , value: ''
                , options: []
                , placeholder: '请输入IP地址'
                , rules: {
                    required: true, message: '设备编号不能为空', trigger: 'blur'
                }
            }, {
                label: '厂区'
                , name: 'factory_no'
                , require: true
                , type: 'select'
                , disabled: false
                , value: ''
                , options: []
                , placeholder: '请选择厂区'
                , rules: {
                    required: true, message: '厂区不能为空', trigger: 'blur'
                }
            }, {
                label: '机型'
                , name: 'm_type'
                , require: true
                , type: 'select'
                , disabled: false
                , value: ''
                , placeholder: '请选择机型'
                , rules: {
                    required: true, message: '机型不能为空', trigger: 'blur'
                }
            }]
            , op: function () {
            }
        }
    }
    , mounted: async function () {
        const me = this;
        me.buttons = await hcCore.getPowerButton();
        await me.getFactories();
        await me.getMacTypes();
        if (me.buttons.hasQuery) {
            me.onQuery();
        }
    }
    , computed: {
        factoryEnum: function () {
            let flags = {};
            this.factories.forEach(function (x) {
                flags[x.sub_value] = x.sub_name;
            });
            return flags;
        }
    }
    , methods: {
        handleButton: function (act_no, row) {
            switch (act_no) {
                case 'Add':
                    this.showEdit('add', '新增');
                    break;
                case 'Edit':
                    this.showEdit('edit', '编辑', row);
                    break;
                case 'Delete':
                    this.onDelete(row);
                    break;
                default:
                    break;
            }
        }
        , onQuery: function () {
            let me = this;
            hcCore.post({
                url: '/Machine/List'
                , param: {
                    page: me.query.page
                    , page_size: me.query.pageSize
                    , company_no: me.query.companyNo
                    , m_no: me.query.mNo
                    , type: me.query.mType
                    , factory: me.query.factoryNo
                }
                , success: function (rsp) {
                    me.tableData.records = rsp.data;
                    me.tableData.total = rsp.total;
                }
            })
        }
        , onPageChange: function (page) {
            this.query.page = page;
            this.onQuery();
        }
        , showEdit: async function (type, title, row) {
            let me = this;
            me.resetParams(me.edit.fields);
            me.edit.title = title;
            let node = me.edit.fields.find(p => p.name == 'factory_no');
            if (node) {
                node.options = me.factories.map(function (x) {
                    return {
                        label: x.sub_name
                        , value: x.sub_value
                    }
                });
            }
            node = me.edit.fields.find(p => p.name == 'm_type');
            if (node) {
                node.options = me.mtypes.map(function (x) {
                    return {
                        label: x.sub_name
                        , value: x.sub_name
                    }
                });
            }
            me.edit.op = type == 'add' ? me.onInsert : (type == 'edit' ? me.onUpdate : function () { });
            if (type == 'edit') {
                if (row) {
                    await hcCore.post({
                        url: '/Machine/Get/' + row.pkid
                        , success: function (rsp) {
                            me.edit.fields.forEach((item, index) => {
                                if (rsp.data[item.name]) {
                                    item.value = rsp.data[item.name];
                                }

                                if (item.name == 'pkid') {
                                    item.disabled = true;
                                }
                            });
                        }
                        , fault: '获得数据失败'
                    });
                }
                else {
                    hcCore.showError('请选择要编辑的记录');
                    return;
                }
            } else {

            }
            me.edit.visible = true;
        }
        , onInsert: function () {
            let me = this;
            me.$refs.edit.$refs[me.edit.formId].validate((valid) => {
                if (valid) {
                    hcCore.post({
                        url: '/Machine/Insert'
                        , param: me.getParams()
                        , success: function (rsp) {
                            hcCore.showSuccess('新增成功');
                            me.tableData.records.push(rsp.data);
                            me.tableData.total += 1;
                            me.edit.visible = false;
                        }
                    });
                } else {
                    return false;
                }
            });
        }
        , onUpdate: function () {
            let me = this;
            me.$refs.edit.$refs[me.edit.formId].validate((valid) => {
                if (valid) {
                    hcCore.post({
                        url: '/Machine/Update'
                        , param: me.getParams()
                        , success: function (rsp) {
                            hcCore.showSuccess('更新成功');
                            let index = me.tableData.records.findIndex(item => item.pkid == rsp.data.pkid);
                            me.$set(me.tableData.records, index, rsp.data)
                            me.edit.visible = false;
                        }
                    });
                } else {
                    return false;
                }
            });
        }
        , onDelete: function (row) {
            let me = this;
            let rows = [];
            if (row) {
                rows = [row];
            }
            else {
                rows = me.$refs.tblRecords.selection;
            }
            if (rows && rows.length > 0) {
                let tips = rows.length == 1 ? ('确认要删除【' + rows[0].pkid + '】这条记录吗') : ('确认要删除选择的【' + rows.length + '】条的记录吗？')
                hcCore.confirm(tips, function () {
                    let arr = rows.map(item => { return item.pkid });
                    let nos = arr.join(',');
                    hcCore.post({
                        url: '/Machine/Delete'
                        , param: {
                            pkids: nos
                        }
                        , success: function (rsp) {
                            hcCore.showSuccess('删除成功');
                            arr.forEach(item => {
                                let index = me.tableData.records.findIndex(node => item == node.pkid);
                                me.tableData.records.splice(index, 1);
                            });
                        }
                    });
                });
            }
            else {
                hcCore.showError('请选择要删除的记录');
            }
        }
        , onExport: function () {
            let paramArr = [];
            paramArr.push('company_no=' + this.query.companyNo)
            paramArr.push('m_no=' + this.query.mNo)

            hcCore.handleExport('/Machine/Export?' + paramArr.join('&'));
        }
        , renderFactory: function (row, column, cellValue, index) {
            return cellValue ? (this.factoryEnum[cellValue] || cellValue) : '';
        }
        , getFactories: function () {
            let me = this;
            hcCore.post({
                url: '/SysSetting/GetFactories'
                , success: function (rsp) {
                    me.factories = rsp.data;
                }
            });
        }
        , getMacTypes: function () {
            let me = this;
            hcCore.post({
                url: '/SysSetting/GetMacTypes'
                , success: function (rsp) {
                    me.mtypes = rsp.data;
                }
            });
        }
        , getParams: function () {
            let param = {};
            this.edit.fields.forEach((item, index) => {
                param[item.name] = item.value;
            });
            return param;
        }
        , resetParams: function (obj) {
            obj.forEach(item => {
                item.value = '';
                item.disabled = false;
            });
        }
        , recordIndex: function (index) {
            return (this.query.page - 1) * this.query.pageSize + index + 1;
        }
    }
});